linux中安装软件
码源安装
开发者会把具体的项目的码源传到linux社区中或者github上,我们可以直接下载源代码,然后编译,最后手动解决软件的依赖问题,软件就可以使用了。
示例:
svn checkout https://svn.example.com/project/trunk
完整流程:
# 1. 下载源码(通常为 .tar.gz、.tar.xz、.zip 或 Git 仓库)
wget https://example.com/package-1.0.tar.gz
# 2. 解压
tar -xzvf package-1.0.tar.gz
cd package-1.0
# 3. 检查依赖并配置(生成 Makefile)
./configure # 默认安装到 /usr/local/
./configure --prefix=/opt/mypackage # 指定安装路径
# 4. 编译(将源代码转为二进制)
make
# 5. 安装(复制文件到系统目录)
sudo make install
# 6. (可选)卸载
sudo make uninstall # 如果 Makefile 支持
RPM 软件包安装
RPM(RPM Package Manager,原名 Red Hat Package Manager)是 Linux 系统上的一种软件打包格式,文件扩展名为.rpm。我们可以通过rpm包安装软件
示例:
sudo rpm -ivh nginx-1.20.1-1.el7.x86_64.rpm
但是软件要运行光有一个软件是不行的,还需要解决相关的依赖问题 ,要把软件依赖的文件分别安装到需要的目录中,才能正常的运行软件。
包管理器安装
在手机上,安装软件都会有一个软件商店,而在liunx中,包管理器就是这个软件商店,它主要解决三大问题:1.自动化安装和卸载2.解决软件的依赖问题3.版本的控制和更新
主流发行版的包管理器:
Debian/Ubuntu:
apt
(.deb
)RHEL/CentOS:
yum
/dnf
(.rpm
)Arch Linux:
pacman
openSUSE:
zypper
linux中的软件是由谁提供的呢?
生态是一个操作系统流行的关键,操作系统的好坏是由生态决定的,如果一个系统没有现成的使用方法,什么都需要我们自己动手开发,我们是不会选择使用这样的系统的。在linux社区中,有着许多开发者,他们开发的源码编译成软件会发布到社区中(适应不同机器的版本),我们通过apt(以ubuntu为例)将linux社区中软件安装到我们的linux机器中(适应自己机器的版本)。开源社区中的项目被各个公司使用之后,为了维持基于开源项目开发出的功能的稳定,公司会反哺开源社区,开源是这样一种商业模式。
包管理器安装的流程:
更新软件源
sudo apt update
# 相当于刷新"应用商店"的商品列表
# 会读取 /etc/apt/sources.list 配置
搜索软件源
apt search nginx
# 支持正则表达式:apt search "^nginx-"
安装软件
sudo apt install nginx
# 典型输出:
# Reading package lists... Done
# Building dependency tree... Done
# The following additional packages will be installed:
# libnginx-mod-http-js nginx-common nginx-core
# 自动识别需要安装的12个依赖包
验证安装
which nginx # 查看安装位置
nginx -v # 查看版本
systemctl status nginx # 检查服务状态
卸载软件
sudo apt remove nginx # 保留配置文件
sudo apt purge nginx # 彻底删除(含配置)
sudo apt autoremove # 清理无用依赖
实操
下面我们来实操一下,安装sl:
安装需要root权限,用apt直接安装
xian@hcss-ecs-887f:/$ su
Password:
root@hcss-ecs-887f:/# apt install -y sl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
sl
0 upgraded, 1 newly installed, 0 to remove and 355 not upgraded.
Need to get 12.7 kB of archives.
After this operation, 60.4 kB of additional disk space will be used.
Get:1 https://mirrors.aliyun.com/ubuntu jammy/universe amd64 sl amd64 5.02-1 [12.7 kB]
Fetched 12.7 kB in 0s (48.0 kB/s)
Selecting previously unselected package sl.
(Reading database ... 83585 files and directories currently installed.)
Preparing to unpack .../archives/sl_5.02-1_amd64.deb ...
Unpacking sl (5.02-1) ...
Setting up sl (5.02-1) ...
Processing triggers for man-db (2.10.2-1) ...
Scanning processes...
Scanning linux images...
Running kernel seems to be up-to-date.
No services need to be restarted.
No containers need to be restarted.
No user sessions are running outdated binaries.
No VM guests are running outdated hypervisor (qemu) binaries on this host.
运行结果:
删除:
root@hcss-ecs-887f:/# apt remove -y sl
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
sl
0 upgraded, 0 newly installed, 1 to remove and 355 not upgraded.
After this operation, 60.4 kB disk space will be freed.
(Reading database ... 83607 files and directories currently installed.)
Removing sl (5.02-1) ...
Processing triggers for man-db (2.10.2-1) ...
root@hcss-ecs-887f:/#
下载软件的本质
无论是哪种下载方式都是先将软件从社区,网络中下载到自己的机器,然后再安装。本质上就是一个拷贝的过程,把网络中的软件拷贝到系统的某个目录中(普通用户权限不够,要root权限),不同的目录中装的内容也是不一样的,一个软件运行需要可执行程序,需要对应的库,同时在运行的过程中会产生临时文件和日志信息,这些信息都需要相应的目录下的文件来储存。
下载安装需要root权限,但是需要把r和w权限开放出来,供others使用。因为下载的东西要所有用户都能使用。
镜像
下载任何一个东西都是需要下载链接的,linux系统中会内置软件源,下载时预置的软件源会告诉包管理器(apt)下载地址。软件源就储存在这个目录当中。
xian@hcss-ecs-887f:/$ cat /etc/apt/sources.list
cat /etc/apt/sources.list.d/*.list # 第三方源
deb https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse
# deb https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.aliyun.com/ubuntu/ jammy-proposed main restricted universe multiverse
deb https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb-src https://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse
deb [arch=amd64 signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu jammy stable
deb [arch=amd64] http://nginx.org/packages/ubuntu jammy nginx
xian@hcss-ecs-887f:/$
我们可以看到,软件源并不是ubuntu社区,而是一个阿里云的镜像。这是为什么呢?Linux的服务器是在境外的,而大陆是无法访问的(有限制)。为了不同地区都能高效地使用linux会建立很多镜像站,当官网出问题时,用户也能正常地下载,且比访问官网更加快速(离得近)。
自己内置的软件源也是储存在一个文件中的,不同的系统不同。ubuntu中储存在 /etc/apt/sources.list。
cat /etc/apt/sources.list
更改软件源
软件源是储存在文件中的,更改软件源也就很简单了,把新的路径粘贴到文件中就可以了。
但是为了防止更改的过程中出问题,我们先将之前的软件源复制一下。
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
然后直接用nano或者vim更改文件保存就好。