在内网、生产环境或无法直接访问互联网的 Ubuntu 服务器/桌面环境中,经常需要通过离线方式安装或升级 Firefox。
本文以 Ubuntu 20.04 x86_64 为例,使用 Mozilla 官方提供的 Linux Tarball 安装包,将 Firefox 安装到 /opt/firefox,并通过软链接让系统默认执行新版本。
Mozilla 官方同样支持这种安装方式:将 Firefox 解压后放置到 /opt,再通过 /usr/local/bin/firefox 创建命令入口。
一、环境说明
本文测试环境:
text
操作系统:Ubuntu 20.04
系统架构:x86_64
安装目录:/opt/firefox
命令入口:/usr/local/bin/firefox
首先确认系统架构:
bash
uname -m
如果输出:
text
x86_64
则需要下载 Linux 64 位 Firefox 安装包。
如果是:
text
aarch64
则需要下载 ARM64 版本。
二、下载 Firefox 离线安装包
在一台能够访问互联网的电脑上进入 Mozilla Firefox 官方下载页面,下载对应架构的 Linux 版本。
Mozilla 官方提供的 Linux Tarball 当前使用 .tar.xz 格式。
下载完成后文件类似:
text
firefox-154.0.tar.xz
将安装包通过 U 盘、SCP 或其他方式复制到离线 Ubuntu 主机,例如:
text
/root/firefox-154.0.tar.xz
三、检查系统已有 Firefox
Ubuntu 系统可能已经安装 Firefox,可以先查看当前版本:
bash
firefox --version
例如:
text
Mozilla Firefox 136.0
查看 Firefox 的实际位置:
bash
which firefox
例如:
text
/usr/bin/firefox
还可以查看系统中所有可以匹配到的 Firefox:
bash
type -a firefox
四、离线安装 Firefox
进入安装包所在目录:
bash
cd /root
如果之前已经通过 /opt/firefox 安装过 Firefox,先删除旧目录:
bash
rm -rf /opt/firefox
解压安装包:
bash
tar -xJf firefox-154.0.tar.xz -C /opt
解压完成后检查:
bash
ls -l /opt/firefox
Firefox 主程序位于:
text
/opt/firefox/firefox
Mozilla 官方文档推荐的系统级 Tarball 安装方式同样是将 Firefox 放置到 /opt。
五、验证新版本
直接执行:
bash
/opt/firefox/firefox --version
例如:
text
Mozilla Firefox 154.0
也可以查看 Firefox 的版本配置:
bash
grep -E 'Version|BuildID' /opt/firefox/application.ini
例如:
text
Version=154.0
BuildID=20260812182057
MinVersion=154.0
MaxVersion=154.0
说明新版本 Firefox 已经正确安装。
六、创建 Firefox 命令软链接
为了直接使用:
bash
firefox
启动新版本,需要创建软链接:
bash
ln -sf /opt/firefox/firefox /usr/local/bin/firefox
检查软链接:
bash
ls -l /usr/local/bin/firefox
应该类似:
text
/usr/local/bin/firefox -> /opt/firefox/firefox
查看最终实际路径:
bash
readlink -f /usr/local/bin/firefox
输出:
text
/opt/firefox/firefox
这种 /opt/firefox + /usr/local/bin/firefox 的目录布局也是 Mozilla 官方文档给出的系统级 Tarball 安装方案。
七、解决安装后仍显示旧版本的问题
这是离线升级 Firefox 时比较容易遇到的问题。
例如:
bash
/opt/firefox/firefox --version
显示:
text
Mozilla Firefox 154.0
但是:
bash
firefox --version
仍然显示:
text
Mozilla Firefox 136.0
首先检查:
bash
which firefox
如果已经是:
text
/usr/local/bin/firefox
再检查:
bash
readlink -f "$(which firefox)"
应该指向:
text
/opt/firefox/firefox
此时可以清除 Bash 的命令路径缓存:
bash
hash -r
然后重新检查:
bash
firefox --version
正常应该显示:
text
Mozilla Firefox 154.0
hash -r 的作用是清除 Bash 已缓存的命令路径,让 Shell 重新按照 $PATH 查找命令。
八、检查 PATH 优先级
查看:
bash
echo "$PATH"
Ubuntu 通常类似:
text
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
这里:
text
/usr/local/bin
位于:
text
/usr/bin
之前。
因此系统执行:
bash
firefox
时会优先找到:
text
/usr/local/bin/firefox
最终调用:
text
/opt/firefox/firefox
整体调用关系:
text
firefox
│
└── /usr/local/bin/firefox
│
└── /opt/firefox/firefox
│
└── Firefox 154.0
九、为什么 /usr/bin/firefox 还是旧版本?
执行:
bash
/usr/bin/firefox --version
可能仍然看到:
text
Mozilla Firefox 136.0
这是正常现象。
此时系统实际上存在两个 Firefox:
text
/usr/bin/firefox
└── Ubuntu 原有 Firefox 136.0
/opt/firefox/firefox
└── 手动安装 Firefox 154.0
而:
text
/usr/local/bin/firefox
指向:
text
/opt/firefox/firefox
由于 /usr/local/bin 的 PATH 优先级高于 /usr/bin,因此直接执行:
bash
firefox
使用的是新版本。
可以通过下面的命令确认:
bash
type -a firefox
例如:
text
firefox 是 /usr/local/bin/firefox
firefox 是 /usr/bin/firefox
firefox 是 /bin/firefox
不建议为了切换版本而直接删除 /usr/bin/firefox。
保留 Ubuntu 原来的 Firefox,通过 /usr/local/bin 覆盖默认命令更加稳妥。
十、配置桌面启动图标
如果使用 Ubuntu Desktop,还需要保证应用菜单启动的是 /opt/firefox/firefox。
创建:
bash
sudo tee /usr/share/applications/firefox.desktop >/dev/null <<'EOF'
[Desktop Entry]
Name=Firefox
Comment=Web Browser
Exec=/opt/firefox/firefox %u
Icon=/opt/firefox/browser/chrome/icons/default/default128.png
Terminal=false
Type=Application
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml+xml;x-scheme-handler/http;x-scheme-handler/https;
StartupNotify=true
EOF
设置权限:
bash
sudo chmod 644 /usr/share/applications/firefox.desktop
如果当前已经登录 Ubuntu Desktop,可以注销后重新登录:
bash
gnome-session-quit --logout
Mozilla 官方也提供了标准的 firefox.desktop 文件安装方式,可将其放置到 /usr/local/share/applications。
十一、最终验证
执行:
bash
which firefox
预期:
text
/usr/local/bin/firefox
检查真实路径:
bash
readlink -f "$(which firefox)"
预期:
text
/opt/firefox/firefox
检查版本:
bash
firefox --version
预期:
text
Mozilla Firefox 154.0
完整检查可以使用:
bash
echo "=== Firefox Path ==="
which firefox
echo "=== Real Path ==="
readlink -f "$(which firefox)"
echo "=== Firefox Version ==="
firefox --version
echo "=== System Firefox ==="
/usr/bin/firefox --version 2>/dev/null || true
十二、快速安装命令
如果安装包已经上传到服务器,可以简化为:
bash
rm -rf /opt/firefox
tar -xJf firefox-*.tar.xz -C /opt
ln -sf /opt/firefox/firefox /usr/local/bin/firefox
hash -r
firefox --version
核心流程就是:
text
下载官方 Firefox Tarball
↓
复制到离线 Ubuntu
↓
解压到 /opt/firefox
↓
创建 /usr/local/bin/firefox 软链接
↓
hash -r 清除 Shell 命令缓存
↓
firefox --version 验证
总结
对于 Ubuntu 20.04 离线环境,直接使用 Mozilla 官方 Firefox Tarball 是一种简单且适合运维部署的方案。
推荐保持以下目录结构:
text
/opt/firefox/
└── Firefox 新版本程序
/usr/local/bin/firefox
└── /opt/firefox/firefox
/usr/bin/firefox
└── Ubuntu 原有 Firefox
这样既可以保留系统原有 Firefox,又能够让命令行和桌面环境优先使用手动部署的新版本。
后续升级也非常简单,只需要备份需要保留的数据后替换 /opt/firefox 目录,再执行:
bash
hash -r
firefox --version
即可确认升级结果。